package com.example.core.service;

import com.example.core.constant.Constant;
import com.example.core.enums.SysEnum;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BaseEnumServiceImpl implements BaseEnumService {

    @Override
    public List<Map<String, String>> getEnumByClassName(String className) throws Exception {
        List<Map<String, String>> list = new ArrayList<>();
        Class<?> clazz = Class.forName(className);
        if (clazz.isEnum() && SysEnum.class.isAssignableFrom(clazz)) {
            Class<? extends SysEnum> enumCLass = (Class<? extends SysEnum>) clazz;
            SysEnum[] enumConstants = enumCLass.getEnumConstants();
            for (SysEnum sysEnum : enumConstants) {
                Map<String, String> map = new HashMap<>();
                map.put(Constant.LABEL, sysEnum.getLabel());
                map.put(Constant.VALUE, sysEnum.getValue());
                list.add(map);
            }
        }
        return list;
    }
}
